package com.yuyou.fn.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.fn.constant.ResultCode;
import com.yuyou.fn.service.IEmailService;
import com.yuyou.fn.service.ISmsService;
import com.yuyou.fn.service.IZySmsService;
import com.yuyou.fn.util.Util;
import com.yuyou.fn.vo.ResultVo;

/**
 * Created by Administrator on 2018/5/29.
 */
@RestController
@RequestMapping("/api/message")
public class MessageController {


    @Resource
    private IEmailService emailService;

    @Resource
    private ISmsService smsService;
    
    @Resource
    IZySmsService zySmsService;
    

    @RequestMapping(value = "/sendEmail", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVo sendEmail(@RequestParam("token")String token,@NotBlank(message="邮箱不能为空")@RequestParam("toAddress") String toAddress,@NotBlank(message="主题不能为空") @RequestParam("subject") String subject, @RequestParam("content") String content, @RequestParam(value = "urls",required = false) String urls, @RequestParam(value = "names",required = false) String names, HttpServletRequest request) {
        ResultVo resultVo=new ResultVo();
        try{
            int code=emailService.sendEmail(token,toAddress,subject,content,Util.toStrings(urls),Util.toStrings(names));
            if (code==0){
                resultVo.setResultCode(ResultCode.success.code);
                resultVo.setMessage(ResultCode.success.message);
            }else if (code==3){
                resultVo.setResultCode(ResultCode.accessFail.code);
                resultVo.setMessage(ResultCode.accessFail.message);
            }
        }catch (Exception e){
            resultVo.setResultCode(ResultCode.error.code);
            resultVo.setMessage(ResultCode.error.message);
        }
        return resultVo;
    }

    @RequestMapping(value = "/sendSMS", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVo sendSMS( @NotBlank(message="手机号码不能为空")@RequestParam("phoneNo")String phoneNo,@NotBlank(message="内容不能为空")@RequestParam("content")String content,@RequestParam("token")String token,HttpServletRequest request) {
        ResultVo resultVo=new ResultVo();
        try{
            int code=zySmsService.sendSMS(Util.toStrings(phoneNo),token,content);
            if (code==0){
                resultVo.setResultCode(ResultCode.success.code);
                resultVo.setMessage(ResultCode.success.message);
            }else if (code==3){
                resultVo.setResultCode(ResultCode.accessFail.code);
                resultVo.setMessage(ResultCode.accessFail.message);
            }else {
                resultVo.setResultCode(code);
                resultVo.setMessage("");
            }
        }catch (Exception e){
            resultVo.setResultCode(ResultCode.error.code);
            resultVo.setMessage(ResultCode.error.message);
        }
        return resultVo;
    }

    @RequestMapping(value = "/sendSMSByNos", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVo sendSMSByNos(@NotBlank(message="手机号码不能为空") @RequestParam("phoneNos")String phoneNos,@NotBlank(message="内容不能为空") @RequestParam("content")String content, @RequestParam("token")String token, HttpServletRequest request) {

        ResultVo resultVo=new ResultVo();
        try{
            int code=zySmsService.sendSMS(Util.toStrings(phoneNos),token,content);
            if (code==0){
                resultVo.setResultCode(ResultCode.success.code);
                resultVo.setMessage(ResultCode.success.message);
            }else if (code==3){
                resultVo.setResultCode(ResultCode.accessFail.code);
                resultVo.setMessage(ResultCode.accessFail.message);
            }
        }catch (Exception e){
            resultVo.setResultCode(ResultCode.error.code);
            resultVo.setMessage(ResultCode.error.message);
        }
        return resultVo;
    }

}
